﻿

var dataGrid_fz,
    orderType_fz = '',
    orderField_fz = '',
    PAGESIZE_fz,
    pageCount_fz,
    rsCount_fz;

var fz_datagridflg = false;




// 请求复诊数据
function requestFzData(queueId) {

    localStorage.setItem("_ISNOSPLIT_PAGE_", "N");

    $.ajax({
        url: _fzGetPatients,
        type: "get",
        data: {
            queueId: queueId
        },
        success: function (data) {

            layer.close(mLoadFlg);

            var result = JSON.parse(data);
            if (result.status == 0) {
                loadFzDataGrid(result.data);
            } else {
                loadDataFailed(result.desc, 11);
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(textStatus);
            console.log(errorThrown);
            loadDataFailed("获取复诊列表数据失败!", 11);
        }
    });
    /*
        var jsonitems = _jsonData.data;
        loadFzDataGrid(jsonitems);*/
}


//构建复诊表格结构
function loadFzDataGrid(d /* String */) {
    var totalNum = 0;
    var _html = "";

    var _queryKey = localStorage.getItem("_P_KEY_") == null ? "" : localStorage.getItem("_P_KEY_");
    var _initSelected = false;
    console.log("请求数据:" + d);

    if (d !== null) {
        var count = 0;
        totalNum = d.length;
        for (var i = 0; i < d.length; i++) {
            var _id = d[i].id;
            var _fznumber = getNotBlankData(d[i].registerId);
            var _hzname = getNotBlankData(d[i].patientName);
            var _doctorname = getNotBlankData(d[i].docName);
            var _treate_status = getNotBlankData(getTreatStatus(d[i].treatStatus));
            var _waitstatus = getNotBlankData(getWaitingStatus(d[i].waitingStatus));
            var _treate_stage_status = getNotBlankData(getTreatmentStatus(d[i].treatmentStage));
            var _treate_end_status = getNotBlankData(getEndStatus(d[i].treatmentEndStatus));
            var _recall_status = getNotBlankData(getRecallStatus(d[i].isNumRecall));
            var _green_status = getNotBlankData(getGreenStatus(d[i].isGreenChannel));
            var _ys_edit_time = getNotBlankData(d[i].delayEditTime);
            var _ys = getNotBlankData(d[i].delayTime);
            var _wb = getTimeName(d[i].timeType);
            var _ghtm = getNotBlankData(d[i].registerTime);
            var _jhtm = getNotBlankData(d[i].callingTime);
            var _remark = getNotBlankData(d[i].remarks);
            var pretestStage = getNotBlankData(d[i].pretestStage);

            var hznameStyle = "background: #FFFFFF"
            if (showColorButtons()) {
                if (pretestStage == "1") {
                    hznameStyle = "background: #fe0000";
                } else if (pretestStage == "2") {
                    hznameStyle = "background: #fffa08";
                } else if (pretestStage == "3") {
                    hznameStyle = "background: #33cc00";
                }
            }
            count++;

            var _inputRadio = "";
            if (_queryKey == _id) {

                localStorage.setItem("_P_KEY_", "");
                _initSelected = true;
                _inputRadio = "<input  type='radio' checked='checked' name='sel_radio' id='" + _id + "' name='fz_" + _fznumber + "' onclick='select_fz_row(this);' " +
                "data-treate_status='" + d[i].treatStatus + "' data-wait_status='" + d[i].waitingStatus + "' " +
                "data-treate_stage_status='" + d[i].treatmentStage + "' data-treate_end_status='" + d[i].treatmentEndStatus + "' " +
                "data-recall_status='" + d[i].isNumRecall + "' data-green_status='" + d[i].isGreenChannel + "' " +
                "data-color_status='" + d[i].pretestStage + "' data-doctorId='" + d[i].doctorId + "' data-doctorName='" + d[i].doctorName + "'/>";

            } else {
                _inputRadio = "<input  type='radio' name='sel_radio' id='" + _id + "' name='fz_" + _fznumber + "' onclick='select_fz_row(this);' " +
                "data-treate_status='" + d[i].treatStatus + "' data-wait_status='" + d[i].waitingStatus + "' " +
                "data-treate_stage_status='" + d[i].treatmentStage + "' data-treate_end_status='" + d[i].treatmentEndStatus + "' " +
                "data-recall_status='" + d[i].isNumRecall + "' data-green_status='" + d[i].isGreenChannel + "' " +
                "data-color_status='" + d[i].pretestStage + "' data-doctorId='" + d[i].doctorId + "' data-doctorName='" + d[i].doctorName + "'/>";
            }

            _html += "<tr>";
            _html += "<td><span >" + _inputRadio + "</span></td>";
            _html += "<td><span style='color:#6DA4EA;'>" + count + "</span></td>";
            _html += "<td><span>" + _fznumber + "</span></td>";
            _html += "<td><span style='" + hznameStyle + "'>" + _hzname + "</span></td>";
            _html += "<td><span>" + _doctorname + "</span></td>";
            _html += "<td><span>" + _treate_status + "</span></td>";
            _html += "<td><span>" + _waitstatus + "</span></td>";
            _html += "<td><span>" + _treate_stage_status + "</span></td>";
            /*_html += "<td><span>" + _treate_end_status + "</span></td>";*/
            _html += "<td><span>" + _recall_status + "</span></td>";
            /*_html += "<td><span>" + _green_status + "</span></td>";*/
            _html += "<td><span>" + getDelayDetailTime(_ys_edit_time, _ys) + "</span></td>";
            _html += "<td><span>" + _wb + "</span></td>";
            _html += "<td><span>" + _ghtm + "</span></td>";
            _html += "<td><span>" + _jhtm + "</span></td>";
            _html += "<td style='text-overflow: ellipsis; white-space: nowrap; overflow: hidden;' title='" + _remark + "'><a data-id='" + _id + "' data-remark='" + _remark + "' onclick='set_fz_remark(this);'><span>" + _remark + "</span></a></td>";

            _html += "</tr>";
        }
    }

    $("#t_table_fz_content").html(_html);

    //表头排序
    $('#t_table_fz').tablesort().data('tablesort');
    $('thead th.number').data('sortBy', function (th, td, sorter) {
        return parseInt(td.text(), 10);
    });

    //更新页面脚标
    $('#patientNum').html("当前队列总共" + totalNum + "位患者!");
    //是否有搜索，需要初始化选中效果
    if (_initSelected) {
        var _t = $("#" + _queryKey);
        select_fz_row(_t);
    }
}


//选择一行
function select_fz_row(t) {
    //console.log($(t).prop("checked"));
    var _ischeked = $(t).prop("checked");


    $("#t_table_fz_content tr").each(function () {
        //console.log($(this).children("td:last"));
        $(this).children("td:last").find("span").css({
            "border-right": "1px solid #FFFFFF",
            //            "background-color": "#F2F3F3"
        });
    });


    //设置本行变色
    if (_ischeked) {
        $(t).parent().parent().find("span").css({
            "border-top": "1px solid #3598db",
            "border-bottom": "1px solid #3598db",
            "border-left": "1px solid #3598db",
            //            "background-color": "#F2F3F3"
        });

        $(t).parent().parent().nextAll().each(function () {
            $(this).find("span").css({
                "border-top": "1px solid #3598db",
                "border-bottom": "1px solid #3598db",
                //                "background-color": "#F2F3F3"
            });
        });

        $(t).parent().parent().parent().prevAll().each(function () {
            $(this).find("span").css({
                "border-top": "",
                "border-bottom": "",
                "border-left": "",
                //                "background-color": "#FFFFFF"
            });
        });

        $(t).parent().parent().parent().nextAll().each(function () {
            $(this).find("span").css({
                "border-top": "",
                "border-bottom": "",
                "border-left": "",
                //                "background-color": "#FFFFFF"
            });
        });
    }
    /*        //取消本行变色
        else {

            $(t).parent().parent().find("span").css({
                "border-top": "",
                "border-bottom": "",
                "border-left": "",
                "background-color": "#FFFFFF"
            });

            $(t).parent().parent().nextAll().each(function () {
                $(this).find("span").css({
                    "border-top": "",
                    "border-bottom": "",
                    "background-color": "#FFFFFF"
                });
            });
            $(t).parent().parent().nextAll().last().find("span").css({
                "border-right": "",
                "background-color": "#FFFFFF"
            });
        }*/

    // 根据选中状态，设置【按钮】的可用状态
    setButtonStatus(t);
}

// 对指定患者设置备注
function set_fz_remark(_this) {
    var id = $(_this).attr("data-id");
    var remark = $(_this).attr("data-remark");

    var win_name = "remarkWin";
    layer.open({
        id: win_name,
        type: 2,
        title: false,
        shadeClose: false,
        closeBtn: 0,
        area: ['400px', '200px'],
        content: '../window/remark_win.html?id=' + id + "&remark=" + remark,
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            var frameId = document.getElementById(win_name).getElementsByTagName('iframe')[0].id;
            var result = $('#' + frameId)[0].contentWindow.updateRemark();
            console.log(result);
            if (result != false) {
                parent.layer.close(index);
                refreshTab();
            }
        },
        cancel: function () {

        }
    });
}





